/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerConfig.java
 *
 * Created on Apr 4, 2011, 11:40:45 AM
 */

package gui;

import Config.ConfigXML;
import Config.Localization;
import da.MSSQLConnection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Administrator
 */
public class ServerConfig extends javax.swing.JPanel {
    private String dataName;
    private String serverName;
    private String userName;
    private String passWord;
    private String port;
    ConfigXML config = null;

    /** Creates new form ServerConfig */
    public ServerConfig() throws UnsupportedLookAndFeelException {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        config = new ConfigXML();
        config.readXML();
        txtDataName.setText(config.getDataName());
        txtServerName.setText(config.getServerName());
        txtUser.setText(config.getUserName());
        txtPass.setText(config.getPassWord());
        txtPort.setText(config.getPort());
        btnDefault.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtServerName = new javax.swing.JTextField();
        txtDataName = new javax.swing.JTextField();
        txtPort = new javax.swing.JTextField();
        txtUser = new javax.swing.JTextField();
        txtPass = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        btnDefault = new javax.swing.JButton();
        btnTest = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("SQLServer Name:");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 30, -1, -1));

        jLabel2.setText("Database Name:");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 60, -1, -1));

        jLabel3.setText("Port:");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 90, -1, -1));

        jLabel4.setText("Username:");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 120, -1, 20));

        jLabel5.setText("PassWord");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 150, -1, -1));
        add(txtServerName, new org.netbeans.lib.awtextra.AbsoluteConstraints(248, 28, 140, -1));

        txtDataName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDataNameActionPerformed(evt);
            }
        });
        add(txtDataName, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 60, 140, -1));
        add(txtPort, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 90, 140, -1));
        add(txtUser, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 120, 140, 20));

        txtPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPassActionPerformed(evt);
            }
        });
        add(txtPass, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 150, 140, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel6.setForeground(new java.awt.Color(51, 51, 255));
        jLabel6.setText("SERVER CONFIG");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 130, 20));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/config.png"))); // NOI18N
        add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 130, 139));

        btnDefault.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Default.png"))); // NOI18N
        btnDefault.setText("Default");
        btnDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefaultActionPerformed(evt);
            }
        });
        add(btnDefault, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 180, 60, -1));

        btnTest.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/check.png"))); // NOI18N
        btnTest.setText("Test");
        btnTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTestActionPerformed(evt);
            }
        });
        add(btnTest, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 180, 80, -1));

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/save.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        add(btnSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 180, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void btnDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDefaultActionPerformed
        // TODO add your handling code here:
        txtDataName.setText("PhongKhamDB");
        txtUser.setText("sa");
        txtPass.setText("789123");
        txtServerName.setText("localhost");
        txtPort.setText("1433");

        this.dataName = "PhongKhamDB";
        this.serverName = "localhost";
        this.userName = "sa";
        this.passWord = "789123";
        this.port = "1433";
        config = new ConfigXML(this);
        config.createXML();
        JOptionPane.showMessageDialog(this,Localization.getMessage("Msg3"));
    }//GEN-LAST:event_btnDefaultActionPerformed

    private void txtDataNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDataNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDataNameActionPerformed

    private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPassActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        this.dataName = txtDataName.getText();
        this.userName = txtUser.getText();
        this.passWord = new String(txtPass.getPassword());
        this.serverName = txtServerName.getText();
        this.port =txtPort.getText();
        config = new ConfigXML(this);
        config.createXML();
        JOptionPane.showMessageDialog(this,Localization.getMessage("Msg3"));
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTestActionPerformed
        MSSQLConnection cn=null;
        try {
            // TODO add your handling code here:
            cn = new MSSQLConnection(txtServerName.getText(), txtUser.getText(), new String(txtPass.getPassword()), txtDataName.getText(), Integer.parseInt(txtPort.getText()));
            if (cn.getConnection() == null) {
                JOptionPane.showMessageDialog(this,Localization.getMessage("Error3"), "ERROR", JOptionPane.WARNING_MESSAGE);
                
            } else {
                JOptionPane.showMessageDialog(this,Localization.getMessage("Msg1"),"Notice",JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            //JOptionPane.showMessageDialog(this, Localization.getMessage("Error3"), "ERROR", JOptionPane.WARNING_MESSAGE);
           // Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        } finally {
            try {
                cn.myClose();
            } catch (SQLException ex) {
                Logger.getLogger(ServerConfig.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnTestActionPerformed

    public String getDataName() {
        return dataName;
    }

    public void setDataName(String dataName) {
        this.dataName = dataName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDefault;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnTest;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField txtDataName;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServerName;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables

}
